package ua.kpi_java_training5.kereia.module2;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Alex Kereia
 * Date: 30.05.13
 * Time: 0:22
 * To change this template use File | Settings | File Templates.
 */
public class Aquarium {
    private int BORN_MARKER = 3;
        int width, length;
        ArrayList<Victim> victims;
        ArrayList<Predator> predators;
    public Aquarium() {
        victims = new ArrayList<Victim>();
        predators = new ArrayList<Predator>();
    }
    public static void main(String[] args) {
        Aquarium aquarium = new Aquarium();
        aquarium.startCondition();
        System.out.println("****************************");
        aquarium.lifeLoop(10);
        aquarium.outputStatistic();
    }
    /**
     * Method for input initial condition
     */
    public void startCondition() {
        crateAquarium();
        generateFishes("VICTIM", returnFromConsole("Put number of victims:"));
        generateFishes("PREDATOR", returnFromConsole("Put number of predators:"));
    }

    /**
     * Method which put limit of aquarium
     */
    public void crateAquarium() {
        width = returnFromConsole("Put value of aquarium width:");
        length = returnFromConsole("Put value of aquarium length:");
    }
    /**
     * Return int value, which print in command line
     * @param textBefore
     * @return
     */
    public static int returnFromConsole(String textBefore) {
        System.out.print(textBefore);
        return new Scanner(System.in).nextInt();
    }
    /**
     * Method which generate fishes in random location, inside aquarium
     * @param type
     * @param number
     */
    public void generateFishes(String type, int number) {
        for (int i = 1;i <= number;i++) {
            int widthLocation = new Random().nextInt(width+1);
            int lengthLocation = new Random().nextInt(length+1);
            while( widthLocation  == 0 || lengthLocation == 0 ) {
                widthLocation = new Random().nextInt(width+1);
                lengthLocation = new Random().nextInt(length+1);
            }
            if (type == "PREDATOR") {
                bornFish(new Predator(widthLocation, lengthLocation, width, length));
            } else if (type == "VICTIM") {
                bornFish(new Victim(widthLocation, lengthLocation, width, length));
            }
        }
    }
    /**
     * Method for born Victim
     */
    public void bornFish(Victim v) {
        victims.add(v);
    }
    /**
     * Method for born Predator
     */
    public void bornFish(Predator p) {
        predators.add(p);
    }
    /**
     * Method which control life loop in aquarium
     * @param loops
     */
    public void lifeLoop(int loops) {
        for (int i = 0;i < loops;i++) {
            for (Victim v: victims) {
                v.go();
            }
            additionalFish(aliveFish());
            steps++;
            for (Predator p: predators) {
                p.go(bestDirection(p), a);
            }
            findMurder();
        }

    }
    static int wasBorn = 0;
    public static int steps = 0;
    /**
     * Method which create born Victims, and count that
     * @param aliveFish
     */
    public void additionalFish(int aliveFish) {
        if(steps%BORN_MARKER == 0 && steps != 0) {
            wasBorn += aliveFish;
            generateFishes("VICTIM", aliveFish);
        }
    }
    /**
     * Count alive victims which remaining after life loop
     * @return
     */
    public int aliveFish() {
        int aliveFish = 0;
        for (Victim v: victims) {
            if (v.getLiveStatus()) {
                aliveFish++;
            }
        }
        return aliveFish;
    }
    private static int a;
    private static int bestWidthSteps;
    private static int bestLengthSteps;
    /**
     * Sets best direction for Predators
     * @param f
     * @return
     */
    public int bestDirection(Fish f) {
        int predatorWidth = f.getWidth();
        int predatorLength = f.getLength();
        chooseVictim();
        if(predatorLength > bestLengthSteps) {
            a = Math.abs(predatorLength - bestLengthSteps);
            return 4;
        } else if(predatorWidth > bestWidthSteps) {
            a = Math.abs(predatorWidth - bestWidthSteps);
            return 2;
        } else if(predatorLength < bestLengthSteps) {
            a = Math.abs(bestLengthSteps - predatorLength);
            return 3;
        } else if(predatorWidth < bestWidthSteps) {
            a = Math.abs(bestWidthSteps - predatorWidth);
            return 1;
        }
        return -1;
    }
    private static int margers = 0;
    /**
     * Find place where coordinates is equal and death victims in that coordinates
     */
    public void findMurder() {
        for (Predator p: predators) {
            for (Victim v: victims) {
                if (p.getWidth() == v.getWidth() && p.getLength() == v.getLength()) {
                    v.death();
                    margers++;
                }
            }
        }
    }
    /**
     * Random choose victims for predators
     */
    public void chooseVictim() {
        int choseVictim;
        do {
            choseVictim = new Random().nextInt(victims.size());
            bestWidthSteps = victims.get(choseVictim).getWidth();
            bestLengthSteps = victims.get(choseVictim).getLength();
        } while (!(victims.get(choseVictim).getLiveStatus()));
    }
    /**
     * Output statistic
     */
    public void outputStatistic() {
        System.out.println("-- STATISTIC --");
        System.out.println("In the begining was " +(victims.size() - (wasBorn)) + " victims");
        System.out.println("In the begining was "+predatorsFish() + " predators");
        System.out.println("Was killed " + margers);
        System.out.println("Was born "+wasBorn+" victims");
        System.out.println("Now is "+(victims.size() - margers)+" victims in aquarium");

    }
    /**
     * Count all predators
     * @return
     */
    public int predatorsFish() {
        return predators.size();
    }


















}
